package com.common.baselibrary.state;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.FrameLayout;

import com.common.baselibrary.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class DataStateLayout extends FrameLayout {

    private View emptyStateView;
    private View errorStateView;
    private View loadingStateView;
//    private View netWorkView;
    private View contentView;
    private AlphaAnimation contentAnimation;

    private int layoutDefaultShowing;

    public DataStateLayout(@NonNull Context context) {
        this(context, null);
    }

    public DataStateLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DataStateLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context, attrs);
    }

    private void initialize(@NonNull Context context, @Nullable AttributeSet attrs) {
        try {
            if (attrs != null) {
                TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DataStateLayout);
                int emptyLayoutRes = typedArray.getResourceId(R.styleable.DataStateLayout_layoutEmpty, 0);
                if (emptyLayoutRes != 0) {
                    emptyStateView = LayoutInflater.from(context).inflate(emptyLayoutRes, this, false);
                    if (emptyStateView != null)
                        addView(emptyStateView);
                }
                int errorLayoutRes = typedArray.getResourceId(R.styleable.DataStateLayout_layoutError, 0);
                if (errorLayoutRes != 0) {
                    errorStateView = LayoutInflater.from(context).inflate(errorLayoutRes, this, false);
                    if (errorStateView != null) {
                        addView(errorStateView);
                        View tryAgainView = errorStateView.findViewById(R.id.data_state_layout_try_again);
                        if (tryAgainView != null)
                            tryAgainView.setOnClickListener(new OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (onTryAgainListener != null)
                                        onTryAgainListener.onTryAgain();
                                }
                            });
                    }
                }
                int loadingLayoutRes = typedArray.getResourceId(R.styleable.DataStateLayout_layoutLoading, 0);
                if (loadingLayoutRes != 0) {
                    loadingStateView = LayoutInflater.from(context).inflate(loadingLayoutRes, this, false);
                    if (loadingStateView != null)
                        addView(loadingStateView);
                }

                //todo 网络加载视图异常资源
                int netLayoutRes = typedArray.getResourceId(R.styleable.DataStateLayout_layoutNetError, 0);

//            if (netLayoutRes != 0) {
//                netWorkView = LayoutInflater.from(context).inflate(netLayoutRes, this, false);
//            }
//            if (netWorkView != null)
//                addView(netWorkView);

                typedArray.recycle();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showLoadingLayout() {
        showStateView(loadingStateView);
    }




    /**
     * 展示布局错误
     */
    public void showErrorLayout() {
        showStateView(errorStateView);
    }

    /**
     * 展示布局的空布局
     */
    public void showEmptyLayout() {
        showStateView(emptyStateView);
    }

    /**
     * 展示布局的内容
     */
    public void showContentLayout() {
        showStateView(contentView);
    }

    /**
     * 展示布局网络异常
     */
//    public void showLayoutNetLayout() {
//        showStateView(netWorkView);
//    }

    private void showStateView(View view) {
        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            int visibility = childView == view ? VISIBLE : INVISIBLE;
            if (childView == contentView && visibility == VISIBLE) {
                childView.setAnimation(getContentAnimation());
            }
            childView.setVisibility(visibility);
        }
    }

    private Animation getContentAnimation() {
        if (contentAnimation == null) {
            contentAnimation = new AlphaAnimation(0f, 1f);
            contentAnimation.setDuration(400);
        }
        return contentAnimation;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (getChildCount() > getStateLayoutCount() + 1) {
            throw new IllegalStateException("布局文件中直接子布局数目不能大于1");
        }
        if (getChildCount() > getStateLayoutCount()) {
            contentView = getChildAt(getChildCount() - 1);
        }
        showStateView(null);
        if (layoutDefaultShowing == 0) {
            if (contentView != null)
                contentView.setVisibility(VISIBLE);
        } else if (layoutDefaultShowing == 2) {
            if (loadingStateView != null)
                loadingStateView.setVisibility(VISIBLE);
        }
    }

    private int getStateLayoutCount() {
        int count = 0;
        if (errorStateView != null)
            ++count;
        if (emptyStateView != null)
            ++count;
        if (loadingStateView != null)
            ++count;
//        if (netWorkView != null)
//            ++count;
        return count;
    }

    @Nullable
    public View getEmptyStateView() {
        return emptyStateView;
    }

    @Nullable
    public View getLoadingStateView() {
        return loadingStateView;
    }

    private OnTryAgainListener onTryAgainListener;

    public void setOnTryAgainListener(OnTryAgainListener onTryAgainListener) {
        this.onTryAgainListener = onTryAgainListener;
    }

    public interface OnTryAgainListener {
        void onTryAgain();
    }
}
